<cfcomponent rest="true" restpath="/event">
	
	<!--- --->
	
	<cffunction name="getEvents" access="remote" httpmethod="GET" restpath="/getEvents">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="lastSync" required="false" default="" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<!--- when using modified_after, include recently deleted items --->
		<cfif arguments.lastSync neq "">
			<cfset arguments.modified_after = arguments.lastSync />
			<cfset arguments.active = "" />
		</cfif>
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelse>
				<!--- set the start date --->
				<cfset arguments.startdate = dateFormat(dateAdd('yyyy',-1,$.date.getDate()),'mm/dd/yyyy') />
				<cfset arguments.enddate = dateFormat(dateAdd('yyyy',2,arguments.startdate),'mm/dd/yyyy') />
				
				<!--- get the events --->
				<cfset arguments.active = "" />
				<cfset local.events = $.run.it('calendarGateway.getEvents',arguments) />
				
				<!--- format the query into the array to pass as result --->
				<cfset local.formattedEvents = [] />
				<cfloop query="local.events">
					<cfset arrayAppend(local.formattedEvents,{
						id=local.events.id,
						description=local.events.description,
						start="#local.events.start_date# #local.events.start_time#",
						stop="#local.events.stop_date# #local.events.stop_time#",
						category=local.events.category,
						parent_id=local.events.parent_id,
						google_event_id=local.events.google_event_id,
						modified=dateTimeFormat(local.events.modified,'mm/dd/yyyy HH:nn:ss'),
						event_categories=local.events.event_categories,
						active=local.events.active
					}) />
				</cfloop>

				<cfset local.response.status = 200 />
				<cfset local.response.content = serializeJSON(local.formattedEvents) />
			</cfif>
			
			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="getEvents",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveEvent" access="remote" httpmethod="GET" restpath="/saveEvent">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="eventID" required="true" type="string" restArgSource="query" />
		<cfargument name="start" required="true" type="string" restArgSource="query" />
		<cfargument name="stop" required="true" type="string" restArgSource="query" />
		<cfargument name="category" required="true" type="string" restArgSource="query" />
		<cfargument name="description" required="true" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelseif not isDate(arguments.start) or not isDate(arguments.stop) or len(arguments.start) neq 19 or len(arguments.stop) neq 19>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "Invalid Date" />
			<cfelse>
				<!--- format the times for saving --->
				<cfset arguments.start = dateTimeFormat(arguments.start,"yyyy-mm-dd HH:nn:ss") />
				<cfset arguments.stop = dateTimeFormat(arguments.stop,"yyyy-mm-dd HH:nn:ss") />
				<cfset local.response.content = $.run.it('calendarService.saveEvent',{
					id=arguments.eventID,
					description=arguments.description,
					start=arguments.start,
					stop=arguments.stop,
					category=arguments.category,
					date="",
					userID=arguments.userID,
					isLocalTime=false
				}) />
				
				<cfset local.response.status = 200 />
			</cfif>

			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="saveEvent",
					args=arguments,
					cfcatch=cfcatch
				) />
				
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />
				
				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->
				
				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="deleteEvent" access="remote" httpmethod="GET" restpath="/deleteEvent">
		<cfargument name="userID" required="true" type="string" restArgSource="query" />
		<cfargument name="eventID" required="true" type="string" restArgSource="query" />
		
		<cfset var local = {} />
		
		<cfset local.user = $.request.setSession(arguments.userID) />
		
		<cfset local.response = {
			status=400,
			headers={},
			content=serializeJSON({error:"No Response"})
		} />
		
		<cftry>
			<cfif local.user.recordcount neq 1>
				<cfset local.response.status = 401 />
				<cfset local.response.content = "Authorization Failed" />
			<cfelseif len(arguments.eventID) neq 32>
				<cfset local.response.status = 400 />
				<cfset local.response.content = "invalid eventID" />
			<cfelse>
				<cfset local.event = $.run.it('calendarService.getEvents',{
					id=arguments.eventID,
					userID=arguments.userID
				}) />
				
				<cfif local.event.recordcount eq 0>
					<cfset local.response.status = 400 />
					<cfset local.response.content = "invalid eventID" />
				<cfelse>
					<cfset $.run.it('calendarService.deleteEvent',{
						eventID=arguments.eventID,
						google_event_id=local.event.google_event_id,
						google_calendar_id=local.event.google_calendar_id		
					}) />
					<cfset local.response.status = 200 />
					<cfset local.response.content = "true" />
				</cfif>
			</cfif>

			<cfset restSetResponse(local.response) />
			
			<cfcatch>
				<cfset application.helpers.log.add(
					name="REST_Error",
					method="deleteEvent",
					args=arguments,
					cfcatch=cfcatch
				) />
				<cfset local.response.status = "500" />
				<cfset local.response.content = "The server has encountered an error processing this request." />

				<!--- <cfsavecontent variable="local.dump">
					<cfdump var="#cfcatch#">
				</cfsavecontent>
				<cfset local.response.content = local.dump /> --->

				<cfset restSetResponse(local.response) />
			</cfcatch>
		</cftry>
		
	</cffunction>
	
	<!--- --->

</cfcomponent>